package com.tsd.jxc.req;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsd.core.datasource.SpringContextUtil;
import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.jxc.JxcConstants;
import com.tsd.jxc.fin.entity.JxcFinancePaymentRequestDetail;
import com.tsd.jxc.fin.entity.JxcFinancePaymentRequestDetailExt;
import com.tsd.jxc.fin.vo.PaymentRequestDetailVo;
import com.tsd.jxc.goods.dao.JxcGoodsMapper;
import com.tsd.jxc.goods.vo.JxcGoodsVo;
import com.tsd.jxc.utils.ClothingUtil;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.utils.AutoInjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @ClassName: JxcFinancePaymentRequestDetailReqParamsVo
 * @Description: TODO
 * @Author: admin
 * @Date: 2023/3/18 14:54
 * @Version: 1.0
 */
public class JxcFinancePaymentRequestDetailReqParamsVo extends JxcFinancePaymentRequestDetail {

    private Map<String, BigDecimal> preset_qty_map;

    private Map<String, String> preset_product_map;

    private Map<String, List<String>> preset_detail_map;

    private String spec_sid;

    private String spec_preset_value;

    public Map<String, BigDecimal> getPreset_qty_map() {
        return preset_qty_map;
    }

    public void setPreset_qty_map(Map<String, BigDecimal> preset_qty_map) {
        this.preset_qty_map = preset_qty_map;
    }

    public Map<String, String> getPreset_product_map() {
        return preset_product_map;
    }

    public void setPreset_product_map(Map<String, String> preset_product_map) {
        this.preset_product_map = preset_product_map;
    }

    public Map<String, List<String>> getPreset_detail_map() {
        return preset_detail_map;
    }

    public void setPreset_detail_map(Map<String, List<String>> preset_detail_map) {
        this.preset_detail_map = preset_detail_map;
    }

    public String getSpec_sid() {
        return spec_sid;
    }

    public void setSpec_sid(String spec_sid) {
        this.spec_sid = spec_sid;
    }

    public String getSpec_preset_value() {
        return spec_preset_value;
    }

    public void setSpec_preset_value(String spec_preset_value) {
        this.spec_preset_value = spec_preset_value;
    }

    public String getSpec_name() {
        return null;
    }

    public List<String> getSpec_preset_value_list() {
        if (!HlpUtils.isEmpty(this.getSpec_preset_value())) {
            return Arrays.asList(this.getSpec_preset_value().split(","));
        }
        return null;
    }

    public static List<JxcFinancePaymentRequestDetailReqParamsVo> transfer(List<JxcFinancePaymentRequestDetailExt> detailExtList, JxcSettingVo settingVo) {
        if (HlpUtils.isEmptyList(detailExtList)) {
            return null;
        }
        List<String> sizeList = ClothingUtil.getClothing_preset_size_list(settingVo);
        List<JxcFinancePaymentRequestDetailReqParamsVo> voList = new ArrayList<>();
        List<JxcFinancePaymentRequestDetailExt> orderList = ListUtil.filter(detailExtList, t -> JxcFinancePaymentRequestDetailExt.SRC_TYPE_ORDER.equals(t.getSrc_type()));
        if (!HlpUtils.isEmptyList(orderList)) {
            JxcGoodsMapper jxcGoodsMapper = SpringContextUtil.getBean(JxcGoodsMapper.class);
            Map<String, Object> params = new HashMap<>();
            params.put("goods_list", ListUtil.map(orderList, JxcFinancePaymentRequestDetailExt::getGoods_sid));
            List<JxcGoodsVo> goodsVoList = jxcGoodsMapper.select4ClothingByPage(params, null);
            for (JxcGoodsVo goodsVo : goodsVoList) {
                List<JxcFinancePaymentRequestDetailExt> filterOrderList = null;//ListUtil.filter(orderList, t -> goodsVo.containsProduct(t.getProduct_sid()));
                if (HlpUtils.isEmptyList(filterOrderList)) {
                    continue;
                }
                JxcFinancePaymentRequestDetailReqParamsVo detailReqParamsVo = JSON.parseObject(JSON.toJSONString(filterOrderList.get(0)), JxcFinancePaymentRequestDetailReqParamsVo.class);
                AutoInjectUtils.clearBaseData(detailReqParamsVo);
                detailReqParamsVo.setSn(filterOrderList.get(0).getSn());
//                detailReqParamsVo.setPreset_product_map(goodsVo.getClothing_preset_product_map());
                if (!HlpUtils.isEmpty(filterOrderList.get(0).getSpecifications())) {
                    detailReqParamsVo.setSpec_info(filterOrderList.get(0).getSpecifications());
                }
//                Map<String, String> presetProductMap = goodsVo.getClothing_preset_product_map();
                Map<String, BigDecimal> presetQtyMap = new HashMap<>();
                Map<String, List<String>> presetDetailMap = new HashMap<>();
                int index = 0;
                for (String size : sizeList) {
                    String productSid = null;//presetProductMap.get(size);
                    if (HlpUtils.isEmpty(productSid)) {
                        index += 1;
                        continue;
                    }
                    List<JxcFinancePaymentRequestDetailExt> detailExts =null;//ListUtil.filter(filterOrderList, t -> productSid.equals(t.getProduct_sid()));
                    if (HlpUtils.isEmptyList(detailExts)) {
                        index += 1;
                        continue;
                    }
                    double totalQty = 0D;
                    List<String> detailList = new ArrayList<>();
                    for (JxcFinancePaymentRequestDetailExt detailExt : detailExts) {
                        totalQty = DoubleUtil.add(totalQty, detailExt.getQty());
                        JSONObject object = new JSONObject();
                        object.put("src_parent_sid", detailExt.getSrc_parent_sid());
                        object.put("src_sid", detailExt.getSrc_sid());
                        object.put("goods_sid", detailExt.getGoods_sid());
                        object.put("goods_name", detailExt.getGoods_name());
                        object.put("qty", detailExt.getQty());
                        object.put("price", detailExt.getPrice());
                        if (!HlpUtils.isEmpty(detailExt.getId()) && !HlpUtils.isEmpty(detailExt.getSid())) {
                            object.put("id", detailExt.getId());
                            object.put("sid", detailExt.getSid());
                        }
                        detailList.add(object.toJSONString());
                    }
                    presetQtyMap.put("preset_qty_" + index, BigDecimal.valueOf(totalQty));
                    presetDetailMap.put(size, detailList);
                    index += 1;
                }
                detailReqParamsVo.setPreset_qty_map(presetQtyMap);
                detailReqParamsVo.setPreset_detail_map(presetDetailMap);
                double totalQty = 0D, totalAmount = 0D;
                for (JxcFinancePaymentRequestDetailExt detailExt : filterOrderList) {
                    totalQty = DoubleUtil.add(totalQty, detailExt.getQty());
                    totalAmount = DoubleUtil.add(totalAmount, detailExt.getAmount());
                }
                detailReqParamsVo.setQty(BigDecimal.valueOf(totalQty));
                detailReqParamsVo.setAmount(BigDecimal.valueOf(totalAmount));
                voList.add(detailReqParamsVo);
            }
        }
        List<JxcFinancePaymentRequestDetailExt> fundList = ListUtil.filter(detailExtList, t -> JxcFinancePaymentRequestDetailExt.SRC_TYPE_FUND.equals(t.getSrc_type()));
        if (!HlpUtils.isEmptyList(fundList)) {
            for (JxcFinancePaymentRequestDetailExt detailExt : fundList) {
                JxcFinancePaymentRequestDetailReqParamsVo detailReqParamsVo = JSON.parseObject(JSON.toJSONString(detailExt), JxcFinancePaymentRequestDetailReqParamsVo.class);
                voList.add(detailReqParamsVo);
            }
        }
        voList = ListUtil.sorted(voList, Comparator.comparing(JxcFinancePaymentRequestDetailReqParamsVo::getThe_date).thenComparing(JxcFinancePaymentRequestDetailReqParamsVo::getSn));
        return voList;
    }

    public PaymentRequestDetailVo convertExportVo() {
        PaymentRequestDetailVo exportVo = new PaymentRequestDetailVo();
        exportVo.setThe_date(HlpUtils.date2String(this.getThe_date(), HlpUtils.Format_Yyyy_M_d_2));
        exportVo.setThe_date_long(this.getThe_date().getTime());
        exportVo.setGoods_name(this.getGoods_name());
//        exportVo.setSpec_info(this.getClothing_color_name());
        exportVo.setQty(this.getQty());
        exportVo.setPrice(this.getPrice());
        exportVo.setAmount(this.getAmount());
        exportVo.setPayment_amount(this.getPayment_amount());
        exportVo.setRemain_amount(this.getRemain_amount());
        exportVo.setNote_info(this.getNote_info());
        exportVo.setUnit(this.getUnit());
        return exportVo;
    }
}
